# coding = utf-8
from concurrent.futures import ThreadPoolExecutor

import requests

pool = ThreadPoolExecutor(10)
url = "http://www.xinfadi.com.cn/getPriceData.html"

cookies = {
}

headers = {
    "Accept": "*/*",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Origin": "http://www.xinfadi.com.cn",
    "Pragma": "no-cache",
    "Referer": "http://www.xinfadi.com.cn/priceDetail.html",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
    "X-Requested-With": "XMLHttpRequest",
}

data = {
    "limit": "20",
    "current": "2",
    "pubDateStartTime": "",
    "pubDateEndTime": "",
    "prodPcatid": "",
    "prodCatid": "",
    "prodName": "",
}

data_list = []
for i in range(1, 50):
    data['current'] = i
    data_list.append(data.copy())


def page_get(data):
    response = requests.post(url, headers=headers, data=data, cookies=cookies)
    for i in response.json()['list']:
        print(i)
    lock.release()


import time
a = time.time()
# for i in data_list:
#     pool.submit(page_get, i) # 第一个写函数 ，后面都是传递参数
# pool.shutdown(True)

import threading

lock = threading.BoundedSemaphore(10)
l = []
for i in data_list:
    lock.acquire()
    t = threading.Thread(target=page_get, args=(i,))
    l.append(t)
    t.start()

for i in l:
    i.join()

b = time.time()
print(b - a)  # 6.938811540603638 7.270256519317627 # 7


# 在限制线程的个数上，线程阻塞，启动运行 线程池比普通线程方便很多


